package com.dunka.demo.util;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Dunka
 * @Description 两个数组元素比较，找出新增和减少
 * @Date 10:33   2019/7/26
 * @ClassName ListComapre
 */
public class ListComapre {

    /***
    * @Description: aList:更新之后的数组，bList：原来的数组，检查更新之后，增加的元素
    * @Param: [aList, bList]
    * @return: java.util.List<E>
    * @Author: Dunka
    * @Date: 2019/7/26 14:24
    */
    public static <E>List<E> getAddaListThanbList(List<E> aList,List<E> bList){
        List<E> addList = new ArrayList<E>();
        for(int i=0; i<aList.size(); i++){
            if(!listContains(bList,aList.get(i))){
                addList.add(aList.get(i));
            }
        }
        return addList;
    }
    /***
    * @Description: aList:更新之后的数组，bList：原来的数组，检查更新之后，减少的元素
    * @Param: [aList, bList]
    * @return: java.util.List<E>
    * @Author: Dunka
    * @Date: 2019/7/26 14:29
    */
    public static <E>List<E> getDeleteaListThanbList(List<E> aList,List<E> bList) {
        List<E> deleteList = new ArrayList<E>();
        for (int i = 0; i < bList.size(); i++) {
            if (!listContains(aList,bList.get(i))){
                deleteList.add(bList.get(i));
            }
        }
        return deleteList;
    }

    /***
    * @Description: 用于判断元素e是否包含在数组里
    * @Param: [bList, e]
    * @return: boolean
    * @Author: Dunka
    * @Date: 2019/7/26 14:26
    */
    private static <E> boolean listContains(List<E> bList, E e) {
        if(bList==null || e == null){
            return false;
        }
        if(bList.isEmpty()){
            return false;
        }
        for (E e1 : bList) {
            if(e.equals(e1)){
                return true;
            }
        }
        return false;
    }


}
